package top.hkyzf.study.leetcode.string;

import org.junit.Test;

import java.util.PriorityQueue;

/**
 * @author 朱峰
 * @date 2022-2-7 9:01
 */
public class 最长快乐字符串_1405 {
    public String longestDiverseString(int a, int b, int c) {
        PriorityQueue<int[]> queue = new PriorityQueue<>((o1, o2) -> o2[1]-o1[1]);
        StringBuilder sb = new StringBuilder();
        if (a > 0) {
            queue.offer(new int[]{0,a});
        }
        if (b > 0) {
            queue.offer(new int[]{1,b});
        }
        if (c > 0) {
            queue.offer(new int[]{2,c});
        }
        while (!queue.isEmpty()) {
            int[] ints = queue.poll();
            if (queue.size() > 0) {
                // 前两个字符等于当前字符
                if (sb.length() > 1 && sb.charAt(sb.length()-1)-'a' == ints[0] && sb.charAt(sb.length()-2)-'a' == ints[0]) {
                    int[] next = queue.poll();
                    sb.append((char)(next[0]+'a'));
                    if (--next[1] != 0) {
                        queue.offer(next);
                    }
                    queue.offer(ints);
                } else {
                    sb.append((char)(ints[0]+'a'));
                    if (--ints[1] != 0) {
                        queue.offer(ints);
                    }
                }
            } else {
                // 前两个字符等于当前字符
                if (sb.length() > 1 && sb.charAt(sb.length()-1)-'a' == ints[0] && sb.charAt(sb.length()-2)-'a' == ints[0]) {
                    break;
                } else {
                    sb.append((char)(ints[0]+'a'));
                    if (--ints[1] != 0) {
                        queue.offer(ints);
                    }
                }
            }
        }
        return sb.toString();
    }

    @Test
    public void testLongestDiverseString () {
        int a = 7;
        int b = 1;
        int c = 0;
        System.out.println(longestDiverseString(a, b, c));
    }
}
